/**
 * 
 */
package pl.kane.autokomp.applications.actionlisteners.file;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFileChooser;

import pl.kane.autokomp.Template;
import pl.kane.autokomp.applications.MainFrame;

/**
 * @author Lukasz Wator
 *
 */
public class SaveAction implements ActionListener {
	 
	public void actionPerformed(ActionEvent arg0) {
		final MainFrame mainFrame = MainFrame.getInstance() ;
		JFileChooser fc = new JFileChooser();
    	fc.setCurrentDirectory(new File("."));
    	int returnVal = fc.showSaveDialog(mainFrame.getFrame());
    	if (returnVal == JFileChooser.APPROVE_OPTION) {
    		 File file = fc.getSelectedFile();
	 			try {
		       PrintWriter out =
		         new PrintWriter (new BufferedWriter (new FileWriter (file)));
		        		out.print(mainFrame.getCode());
			   	
		       out.flush ();
		       out.close ();
		       mainFrame.addToConsole("File "+file.getName()+" saved\n");
	    	   mainFrame.setCurrentFileName(file.getName());
	 			}
    			    catch (IOException e) {
    		    	e.printStackTrace();
    		    }
    		 			 
    	} else {
    		 mainFrame.addToConsole("File seve canceled\n");
        }

       
     }

}
